package jk.zmn.socket.tcp;


import javax.swing.text.TabableView;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 张梦楠
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2018/5/3011:19
 * @blog www.itzmn.com
 *
 * TCP 服务端
 * 1.构建服务端
 * 2.得到数据
 * 3，解析数据
 *
 */
public class Server {

     public static void main(String[] args) throws Exception{
         //创建一个服务
         ServerSocket server = new ServerSocket(8888);

         while (true){
             //当有客户端连接时候，就会创建一个soket
             Socket socket = server.accept();
             InputStream inputStream = socket.getInputStream();
             BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
             int readsize=0;

             byte[] data = new byte[10];

             while ((readsize = bufferedInputStream.read(data))>0){
                 byteArrayOutputStream.write(data,0,readsize);
             }
             System.out.println("客户端跟我说："+byteArrayOutputStream.toString());


             String msg = "客户端你好";
             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
             bufferedOutputStream.write(msg.getBytes());
             bufferedOutputStream.flush();
             //socket.shutdownOutput();
         }
     }

}
